import { CreateElement } from 'vue';
import { throttle } from '@ibizstudio/runtime';
import { PickupView2Base } from '../../../view/pickupview2-base';
import { AppLayoutService } from '../../..';

export class AppPickupView2Base extends PickupView2Base {
  /**
   * 渲染选择视图面板
   *
   * @memberof AppPickupView2Base
   */
  renderMainContent() {
    let { targetCtrlName, targetCtrlParam, targetCtrlEvent } = this.computeTargetCtrlData(this.treeExpBarInstance);
    Object.assign(targetCtrlParam.staticProps, { pickupviewpanel: this.pickupViewInstance });
    return this.$createElement(targetCtrlName, { slot: 'default', props: targetCtrlParam, ref: this.treeExpBarInstance?.name, on: targetCtrlEvent });
  }

  /**
   * 渲染选择视图按钮
   *
   * @memberof AppPickupView2Base
   */
  renderPickButton() {
    if (this.isShowButton) {
      return (
        <card dis-hover={true} bordered={false} class='footer'>
          <row style={{ textAlign: 'right' }}>
            <i-button type='primary' disabled={this.viewSelections.length > 0 ? false : true} on-click={(...params: any[]) => throttle(this.onClickOk, params, this)}>
              {this.containerModel?.view_okbtn?.text}
            </i-button>
            &nbsp;&nbsp;
            <i-button on-click={(...params: any[]) => throttle(this.onClickCancel, params, this)}>{this.containerModel?.view_cancelbtn?.text}</i-button>
          </row>
        </card>
      );
    }
  }

  /**
   * 数据视图渲染
   *
   * @memberof AppPickupView2Base
   */
  render(h: CreateElement) {
    if (!this.viewIsLoaded) {
      return null;
    }
    const targetViewLayoutComponent: any = AppLayoutService.getLayoutComponent(`${this.viewInstance.viewType}-${this.viewInstance.viewStyle}`);
    return h(
      targetViewLayoutComponent,
      {
        props: { viewInstance: this.viewInstance, model: this.model, modelService: this.modelService, viewparams: this.viewparams, context: this.context },
      },
      [this.renderTopMessage(), this.renderBodyMessage(), this.renderToolBar(), this.renderMainContent(), this.renderPickButton(), this.renderBottomMessage()],
    );
  }
}
